package net.xdclass.forum.controller;

import net.xdclass.forum.domain.User;
import net.xdclass.forum.service.UserService;
import net.xdclass.forum.service.impl.UserServiceimpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet(name = "userRegister", urlPatterns = "/user")
public class UserServlet extends BasedServlet{
    UserService userService = new UserServiceimpl();

    //登录
    public void login(HttpServletRequest request, HttpServletResponse response)
    {
        //获取登录的手机号和密码
        String phone = request.getParameter("phone");
        String pwd = request.getParameter("pwd");

        //获取登录的user
        User user = userService.login(phone, pwd);

        //判断是否获取成功
        if(user != null){
            //登录成功设置userLogin
            request.getSession().setAttribute("loginUser", user);
            //跳转页面
            try {
                response.sendRedirect("/topic?method=list&c_id=1");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        else {
            //登录失败设置失败信息
            request.getSession().setAttribute("msg", "用户名或密码不正确");
            //跳转页面
            try {
                request.getRequestDispatcher("/user/login.jsp").forward(request, response);
            } catch (ServletException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }


    //退出登录
    public void logout(HttpServletRequest request, HttpServletResponse response)
    {
        request.getSession().invalidate();

        try {
            request.getRequestDispatcher("/user/login.jsp").forward(request, response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //跳转到登录界面
    }

    public void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = new User();
        //通过BeanUtils.populate(user, map)把前端传过来的用户信息封装到user里面
        Map<String, String[]> map= request.getParameterMap();
        try {
            BeanUtils.populate(user, map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        //执行service层的regiseter方法，
        int i = 0;
        i = userService.register(user);
        if(i > 0) {
            //注册成功,跳转到登录界面
            request.getRequestDispatcher("/user/login.jsp").forward(request, response);
        }
        else {
            //注册失败，跳转到注册界面
            request.getRequestDispatcher("/user/register.jsp").forward(request, response);
        }

    }
}
